package resources

import (
	"TCP/resources/db"
	"strconv"
	"xlib/log"
)

//
const (
	// Record Type
	ETPasswordWrong          = -2
	ETCardFailed             = -1
	ETCardOrPasswordOpenDoor = 0
	ETButtonOpenDoor         = 1
	ETPasswordOpenDoor       = 2
	ETRemoteOpenDoor         = 3
	ETRobotOpenDoor          = 11
	ETRobotCloseDoor         = 12

	// Alarm Type
	ETOpenDoorTimeout   = 0
	ETExceptionOpenDoor = 1
	ETFireAlarm         = 2
	ETBoxTamperAlarm    = 3

	ETUnknownType = 999

	// 找不到对应的type 在原始type上偏移
	ETTypeOffset = 1000
)

var recordDesc = map[int]string{
	ETPasswordWrong:          "密码无效",
	ETCardFailed:             "刷卡失败",
	ETCardOrPasswordOpenDoor: "刷卡/卡+密码开门",
	ETButtonOpenDoor:         "按钮开门",
	ETPasswordOpenDoor:       "密码开门",
	ETRemoteOpenDoor:         "", // 在接口调用处生成，包含ip
	ETRobotOpenDoor:          "", //
	ETRobotCloseDoor:         "", //
}

var alarmDesc = map[int]string{
	ETOpenDoorTimeout:   "门开超时",
	ETExceptionOpenDoor: "异常开门",
	ETFireAlarm:         "发现火警",
	ETBoxTamperAlarm:    "机箱被打开",
}

/*
刷卡记录中type、desc参数取值定义：
type=-2, desc="密码无效", (此时 card_no、user_name 为空串"")
type=-1, desc="刷卡失败", (此时 user_name 如无配置则为空串"")
type=0, desc = "刷卡/卡+密码开门"
type=1, desc = "按钮开门"(此时 card_no、user_name 为空串"")
type=2, desc = "密码开门" (此时 card_no、user_name  为空串"")
type=3, desc = "远程开门，指令发送方IP：[ip]" (此时 card_no、user_name 为空串"")
type=11, desc = "机器人开门，指令发送方IP：[ip]"
type=12, desc = "机器人关门，指令发送方IP：[ip]"

告警记录中的alm_desc参数取值范围：
type=0, alm_desc ="门开超时"
type=1, alm_desc = "异常开门"（包括暴力入侵）
type=2, alm_desc ="发现火警"（如果是多门控制器，则只报第1个门。当北向接口出自门控器时，门编号一般就是1， 当北向接口出自门禁系统时，需要门禁系统上报的是：出现火警的门控器的第1个门 在系统中的编号）
*/

var recordAlarmType = map[string][2]int{
	"":    {ETUnknownType, 0},
	"0":   {ETCardOrPasswordOpenDoor, 0},
	"1":   {ETCardFailed, 0},
	"2":   {ETCardFailed, 0},
	"3":   {ETCardFailed, 0},
	"4":   {ETPasswordWrong, 0},
	"5":   {ETCardFailed, 0},
	"10":  {ETCardOrPasswordOpenDoor, 0},
	"11":  {ETCardOrPasswordOpenDoor, 0},
	"20":  {ETCardOrPasswordOpenDoor, 0},
	"21":  {ETCardOrPasswordOpenDoor, 0},
	"22":  {ETCardOrPasswordOpenDoor, 0},
	"23":  {ETCardOrPasswordOpenDoor, 0},
	"24":  {ETPasswordOpenDoor, 0},
	"25":  {ETPasswordOpenDoor, 0},
	"47":  {ETExceptionOpenDoor, 1},
	"48":  {ETExceptionOpenDoor, 1},
	"49":  {ETExceptionOpenDoor, 1},
	"57":  {ETOpenDoorTimeout, 1},
	"59":  {ETButtonOpenDoor, 0},
	"65":  {ETOpenDoorTimeout, 1},
	"72":  {ETFireAlarm, 1},
	"76":  {ETCardFailed, 0},
	"97":  {ETOpenDoorTimeout, 1},
	"201": {ETRemoteOpenDoor, 0},
	"202": {ETRobotOpenDoor, 0},
	"203": {ETRobotCloseDoor, 0},
}

// 向前兼容reserve字段为空的事件类型，通过Content进行判断
var recordAlarmDescType = map[string]string{
	"单卡识别":   "0",
	"无效卡号":   "1",
	"没有时区":   "2",
	"无效时区":   "3",
	"密码错误":   "4",
	"双卡识别错误": "5",
	"出门":     "10",
	"进门":     "11",
	"卡加密码进门": "20",
	"卡加密码出门": "21",
	"双卡进门":   "22",
	"双卡出门":   "23",
	"密码进门":   "24",
	"密码出门":   "25",
	"胁持报警":   "47",
	"防拆报警":   "48",
	"潜反报警":   "49",
	"开门时间太长": "57",
	"出门按钮打开": "59",
	"多人开门超时": "65",
	"火警":     "72",
	"无效门":    "76",
	"出门超时":   "97",
}

func GetRecordAlarmTypeDesc(event *db.AcsDoorEvent) (int, string) {
	t := event.Reserve1
	if t == "" {
		// cover 老代码
		t = recordAlarmDescType[event.Content]
	}
	if v, ok := recordAlarmType[t]; ok {
		var desc string
		//if event.RecordType == define.EventType {
		if v[1] == 0 {
			desc = recordDesc[v[0]]
		} else {
			desc = alarmDesc[v[0]]
		}
		return v[0], desc
	} else {
		RAtype, err := strconv.Atoi(t)
		if err != nil {
			log.Error("invalid type not number", t)
			return ETUnknownType, ""
		}
		return RAtype + ETTypeOffset, ""
	}
}

func GetAlarmState(event *db.AcsDoorEvent) int {
	state, err := strconv.Atoi(event.Reserve2)
	if err != nil {
		// 没有时都算产生(1)
		state = 1
	}
	return state
}

var DoorStatusMap = map[string]int{
	"close":        0,
	"open":         1,
	"normallyOpen": 2,
	//"locked": 3,
	//"unlocked":4,
}

// ACL的运行装状态有4种值
// 关闭,开启,常开,常闭 对应 mdc 正常,常开,常闭
var AclRunStat2MDCStat = map[byte]int{
	0: 0,
	1: 0,
	2: 1,
	3: 2,
}
